#include <stdio.h>
#include <stdbool.h>

bool isMatched(const char *str) {
	char stack[100];
	int top = -1;

	for (int i = 0; str[i] != '\0'; i++) {
		char ch = str[i];
		if (ch == '(' || ch == '[' || ch == '{') {
			stack[++top] = ch;
		} else if (ch == ')' || ch == ']' || ch == '}') {
			if (top == -1) {
				return false;
			}
			char left = stack[top--];
			if ((ch == ')' && left != '(') || (ch == ']' && left != '[') || (ch == '}' && left != '{')) {
				return false;
			}
		}
	}

	return top == -1;
}

int main() {
	char str[50] ;
	int n;
	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		scanf("%s", str);
		if (isMatched(str)) {
			printf("Yes\n");
		} else {
			printf("No\n");
		}
	}


	return 0;
}